<?php
	require(dirname(__FILE__) . '/includes/init.php');
if($_REQUEST['act'] == 'search'){
	if(!empty($_REQUEST['mobile'])){
		$services = service(trim($_REQUEST['mobile']),0);
	}else if(!empty($_REQUEST['consignee'])){
		$services = service(0,trim($_REQUEST['consignee']));
	}
}
elseif($_REQUEST['act'] == 'send'){
	require(dirname(__FILE__) . '/includes/cls_sms.php');
	$sms = new sms();
	$phone = $_REQUEST['phone'];
	//$phone = '13816064684';
	$msg = $_REQUEST['message'];
	$user = $_REQUEST['user'];
	$str = $sms->send($phone,$msg);
	if($str){
		$sql = "update ".$ecs->table('enrols')." set send=1 where order_id='".$_REQUEST['order_id']."'";
		$db->query($sql);
	}else{
		$sql = "update ".$ecs->table('enrols')." set send=2 where order_id='".$_REQUEST['order_id']."'";
		$db->query($sql);
	}
	header("Location: customerservice.php?deal=2");
}
else{
	if($_REQUEST['deal']==1){
		if(!empty($_REQUEST['from_where'])){
			$services = service('','',1,0,$_REQUEST['from_where']);
		}else{
			$services = service('','',1,0);
		}
    }else if($_REQUEST['deal']==2){
		if(!empty($_REQUEST['from_where'])){
			$services = service('','',0,1,$_REQUEST['from_where']);
		}else{
			$services = service('','',0,1);
		}
	}else if($_REQUEST['deal']==3){
		if(!empty($_REQUEST['from_where'])){
			$services = service('','',0,0,$_REQUEST['from_where'],1);
		}else{
			$services = service('','',0,0,0,1);
		}
	}
	else{
		if(!empty($_REQUEST['from_where'])){
			$services = service('','',0,0,$_REQUEST['from_where']);
		}else{
			$services = service();
		}
	}
}

function service($key='',$key1='',$key2=0,$key3=0,$key4=0,$key5=0){
	$sql = "SELECT o.*,e.* FROM " .$GLOBALS['ecs']->table('orders')." as o inner join ".$GLOBALS['ecs']->table('enrols')."as e on o.order_id = e.order_id where 1=1";
	 /* 查询 */
	if($key){
		$sql .= " and o.mobile='".$key."'";
	}
	if($key1){
		$sql .= " and o.user like'%".$key1."%'";
	}
	if($key2){
		$sql .= " and e.ques_status=0";
	}
	if($key3){
		$sql .= " and e.ques_status=2 and e.send=0";
	}
	if($key4){
		$sql .= " and o.from_where like '%".$key4."%'";
	}
	if($key5){
		$sql .= " and e.ques_status=1";
	}
	$sql .=" order by e.enrol_time desc";
	$row = $GLOBALS['db']->getAll($sql);
	  $j=0;
	  foreach($row AS $index=>$rs){
		 $j++;
		 if($rs['send']==0){
			 $row[$index]['send'] = "未通知&nbsp;&nbsp;<a href='javascript:void(0)' onclick='send_message(".$j.")'>发送</a><div class='tab-div' id ='msg_".$j."' style='display:none;position:absolute;width:190px;'><div id='tabbody-div'>
<form method='post' name='msg_form_".$j."' action='customerservice.php' onsubmit='return message_len()'><table align='center'><input name='order_id' type='hidden' value='".$rs['order_id']."'/><input name='user' type='hidden' value='".$rs['user']."' /><input name='phone' type='hidden' value='".$rs['mobile']."' /><input name='act' type='hidden' value='send' /><textarea name='message' id='message'></textarea></table> <div class='button-div'><input class='button' type='submit' value='发送' /><a href='javascript:void(0)'  onclick='close_message(".$j.")'>关闭</a></div></form></div></div>";
		 }elseif($rs['send']==1){
			 $row[$index]['send'] = '成功';
		 }else{
			$row[$index]['send'] = "失败&nbsp;&nbsp;<a href='javascript:void(0)' onclick='send_message(".$j.")'>发送</a><div class='tab-div' id ='msg_".$j."' style='display:none;position:absolute;width:190px;'><div id='tabbody-div'>
<form method='post' name='msg_form_".$j."' action='customerservice.php' onsubmit='return message_len()'><table align='center'><input name='order_id' type='hidden' value='".$rs['order_id']."'/><input name='user' type='hidden' value='".$rs['user']."' /><input name='phone' type='hidden' value='".$rs['mobile']."' /><input name='act' type='hidden' value='send' /><textarea name='message' id='message'></textarea></table> <div class='button-div'><input class='button' type='submit' value='发送' /><a href='javascript:void(0)'  onclick='close_message(".$j.")'>关闭</a></div></form></div></div>";
		 }
		 $sql = "select count(*) as num from ".$GLOBALS['ecs']->table('orders_action')." where order_id='".$rs['order_id']."'";
		 $num = $GLOBALS['db']->getOne($sql);
         //$arr = array();
         /*while ($val = $GLOBALS['db']->fetchRow($res)){
			$arr[$val['action_id']]['contents'] = $val['contents'];
			$arr[$val['action_id']]['do_name'] = $val['do_name'];
		 }*/
		 $row[$index]['num'] = $num;
		 //$row[$index]['actions'] = $arr; 
	  }
	  return $row;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>管理中心 - 售后列表 </title>
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/general.css" rel="stylesheet" type="text/css" />
<link href="styles/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/common.js"></script>
</head>
<body>
<h1>
<span class="action-span"><a href="orders.php">回到订单列表</a></span>
<span class="action-span1"><a target="_top" href="index.php?act=main">管理中心</a> </span><span id="search_id" class="action-span1"> - 售后列表 </span>
<div style="clear:both"></div>
</h1>
<div class="form-div">
  <form action="customerservice.php" method="post" name="searchForm" onsubmit="return search_yz()">
    <img src="images/icon_search.gif" width="26" height="22" border="0" alt="SEARCH" />
    手机<input name="mobile" type="text" id="mobile" size="15">
    收货人<input name="consignee" type="text" id="consignee" size="15">
	<input type="hidden" name="act" value="search" />
    <input type="submit" value=" 搜索 " class="button" />
	<a href="customerservice.php" <?php if(!isset($_GET['deal'])){?>style="background: #192E32"<?php }?>>所有状态</a>
	<a href="customerservice.php?deal=1" <?php if($_GET['deal']==1){?>style="background: #192E32"<?php }?>>待解决</a>
	<a href="customerservice.php?deal=3" <?php if($_GET['deal']==3){?>style="background: #192E32"<?php }?>>解决中</a>
    <a href="customerservice.php?deal=2" <?php if($_GET['deal']==2){?>style="background: #192E32"<?php }?>>已解决未发短信</a>
	&nbsp;&nbsp;<span>订单来源:</span>
	<select name="from_where" onchange="from()" id="from_where">
	 <?php if(isset($_GET['from_where'])&&($_GET['from_where']=='一号店'))?>
	 <option value="0">请选择...</option>
	 <option value="一号店"  <?php if(isset($_GET['from_where'])&&($_GET['from_where']=='一号店')){?> selected <?php }?>>一号店</option>
	 <option value="京东" <?php if(isset($_GET['from_where'])&&($_GET['from_where']=='京东')){?> selected <?php }?>>京东商城</option>
	 <option value="淘宝" <?php if(isset($_GET['from_where'])&&($_GET['from_where']=='淘宝')){?> selected <?php }?>>淘宝商城</option>
	 <option value="QQ" <?php if(isset($_GET['from_where'])&&($_GET['from_where']=='QQ')){?> selected <?php }?>>QQ商城</option>
	</select>
  </form>
</div>
<script type="text/javascript">
function search_yz(){
	var mobile = $("#mobile").val();
	var consignee = $("#consignee").val();
	if(mobile==""&&consignee==""){
		alert("请输入搜索条件");
		return false;
	}else{
		return true;
	}
}
</script>

<!-- 订单列表 -->
  <div class="list-div" id="listDiv">
 
<table cellpadding="3" cellspacing="1">
  <tr>
    <th>类型</th>
    <th>系统订单号</th>
    <th>收货人</th>
    <th>产品</th>
    <th>手机</th>
	<th>问题描述</th>
	<th>订单时间</th>
    <th>登记时间</th>
	<th>是否解决</th>
    <th>解决方式</th>
	<th>短信通知</th>
	<th>解决记录</th>
    <th>操作</th>
  </tr>
  <?php 
  if(count($services)){
	  $i=0;
	foreach($services AS $index => $service){
		$i++;
  ?>
   <tr>
    <td><?php if($service['question_cat']==1){ echo '碎酒';}else if($service['question_cat']==2){ echo '漏发';}else if($service['question_cat']==3){echo '发票';}else if($service['question_cat']==4){echo '地址不对';}else if($service['question_cat']==5){echo '其他';}else{echo '未标类型';}?></td>
    <td><?php echo $service['sys_order_sn']?></td>
    <td><span id="order_<?php echo $i;?>"><?php echo $service['user']?></span><div id="address_<?php echo $i;?>" style="height:20px;margin-top:-1px;position:absolute;color:#fff;background-color:red;display:none"><?php echo $service['address'];?></div></td>
    <td><?php echo $service['product_name']?></td>
    <td><?php echo $service['mobile']?></td>
	<td><?php echo $service['descr'];?></td>
	<td>
	<?php 
	if(preg_match("/[\x7f-\xff]/", $service['order_time'])) {
		echo $service['order_time'];
	}else{
		$n = intval(($service['order_time']-25569)*3600*24); //转换成1970年以来的秒数
		echo gmdate('Y-m-d', $n);//格式化时间
	}?>
	</td>
	<td><?php echo date('Y-m-d H:i:s',$service['enrol_time']);?></td>
	<td><?php if($service['ques_status']==0){echo '待解决';}else if($service['ques_status']==1){echo '解决中';}else if($service['ques_status']==2){echo '已解决';}?></td>
	<td><?php if($service['deal_cat']==0){echo '未选择';}else if($service['deal_cat']==1){echo '退款';}else if($service['deal_cat']==2){echo '补发';}else if($service['deal_cat']==3){echo '其他';}?></td>
	<td><?php echo $service['send'];?></td>
	<td id="td_<?php echo $i;?>"><span><?php echo $service['num'];?></span>
	</td>
    <td><a href="deal.php?sn=<?php echo $service['order_id'];?>">解决</a>&nbsp;&nbsp;<a href="service_record.php?sn=<?php echo $service['order_id'];?>">记录</a></td> 
  </tr>
  <?php } } ?>
  </table>
  </div>
<script type="text/javascript">
var num = <?php echo count($services);?>;
for (i=1;i<=num;i++){
    hide_show_1(i);
	hide_show_2(i);
}
function hide_show_1(i){
	$("#td_"+i).hover(function() {
        $("#contents_"+i).show();  
    },function(){
           $("#contents_"+i).fadeOut(1000);  
        }

   ); 
}
function hide_show_2(i){

	$("#order_"+i).hover(function() {         
        $("#address_"+i).show();  
    },function(){
           $("#address_"+i).fadeOut(1000);  
        }

   ); 
}
function from(){
	var from_where = $("#from_where").val();
	var url = "<?php echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];?>";
	if(url.indexOf("?") > 0 )
	{
		window.location.href = url +'&from_where='+from_where;
	}else{
		window.location.href = url+'?from_where='+from_where;  
	}
}
function send_message(k){
	$("#msg_"+k).css("display","block");
}
function close_message(k){
	$("#msg_"+k).css("display","none");
}
</script>
</body>
</html>